package compiler;

import java.util.HashMap;
import java.util.Map;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
public class SymbolTable {
	private Map<String,Symbol>map;
    private int symbolPosition;

    public SymbolTable(){
		this.map=new HashMap<String,Symbol>();
        symbolPosition=1;//considers static method
    }
	
	public Symbol declare(String name){
		Symbol symbol = new Symbol();	
        symbol.setName(name);
        map.put(name,symbol);
        symbol.setPosition(getSymbolPosition());
        incrementSymbolPosition();
        return symbol;
	}
	
	public boolean isDeclared(String name){
		return map.containsKey(name);
	}
	
	public Symbol getDeclaredSymbol(String name){
		return map.get(name);
	}

    /**
     * Returns position to be assigned to the declared symbol;
     * @return int;
     */
    private int getSymbolPosition() {
        return symbolPosition;
    }

    private void setSymbolPosition(int symbolPosition) {
        this.symbolPosition = symbolPosition;
    }

    private void incrementSymbolPosition(){
        this.symbolPosition++;
    }

    public int getTotalSymbols(){
        return symbolPosition-1;
    }
}
